<template>
  <div>
    <register @changeRegister="isRegister = true" v-if="!isRegister"></register>
    <apply v-if="isRegister"></apply>
  </div>
</template>

<script>
import { setCookies } from '@/libs/auth'
import Register from './components/outsider/register.vue'
import Apply from './components/outsider/apply.vue'
import { getOutsiderInfo } from '@/api/school.js'

export default {
  data() {
    return {
      isRegister: true,
      route: {},
      userInfo: {},
    }
  },
  components: { Register, Apply },
  computed: {},
  watch: {},
  async mounted() {
    // 授权登录
    const { key, card_no, name, mobile } = this.$route.query
    // 用户信息处理
    if (card_no && name && mobile) {
      if (!key) {
        this.$toast('登录失败')
        return
      }
      // 设置1天token有效期
      this.$store.dispatch('user/changeToken', key)
      setCookies('token', key, 43200)
      this.$store.dispatch('user/changeUserInfo', {
        card_no: card_no,
        name: name,
        mobile: mobile,
      })
      this.userInfo = this.$store.state.user.userInfo
    } else {
      this.$store.dispatch('user/changeUserInfo', {})
      const returnUrl = encodeURIComponent(window.location.href)
      window.location.href =
        'https://dalianyqschool.lngov.top' +
        '/api/ac/dalianyqschool/qstbMobileService/redirectToDaLian' +
        '?returnUrl=' +
        returnUrl
    }
    // if (!this.$route.query.card_no) {
    //   this.$toast('缺少参数')
    //   return
    // }
    // if (this.$store.state.user.userInfo.card_no) {
    //   this.userInfo = this.$store.state.user.userInfo
    // } else {
    //   this.$store.dispatch('user/changeUserInfo', {})
    //   this.$store.dispatch('user/changeUserInfo', this.$route.query)
    // }
    this.init()
  },
  methods: {
    async init() {
      const outsiderData = await getOutsiderInfo({
        card_no: this.userInfo.card_no,
      })
      if (outsiderData.data.errcode == 0) {
        if (outsiderData.data.data.name) {
          this.isRegister = true
        } else {
          this.isRegister = false
        }
      }
    },
  },
}
</script>

<style lang="less" scoped>
</style>
